// eee.Sheffield.PZ.Imaging
//
// Copyright ? Ping Zou, 2007
// sg71.cherub@gmail.com

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using eee.Sheffield.PZ.Math;

namespace eee.Sheffield.PZ.Imaging.ImageFilter
{
    /// <summary>
    /// Threshold
    /// </summary>
    public class Threshold : ImageFilter//, IGraylevelImageFilter
    {
        #region Fields
        private int _threshold;
        #endregion

        #region Properties
        public int ThresholdValue { get { return _threshold; } }
        #endregion

        #region Constructor
        public Threshold(int threshold)
        {
            _threshold = threshold;
            _flag = "[Image -> Threshold -> Threshold]";
        }
        #endregion

        #region Apply filter        
        protected override PZMath_matrix ApplyDoubleGraylevelImageFilter(PZMath_matrix srcMatrix, out string logText)
        {
            string thresholdLog = "";

            // get source
            int width = srcMatrix.ColumnCount;
            int height = srcMatrix.RowCount;

            // create dstMatrix
            PZMath_matrix dstMatrix = new PZMath_matrix(height, width);

            // threshold
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    // ivert each pixel
                    dstMatrix[y, x] = srcMatrix[y, x] <= _threshold ? 0 : 255;
                }
            }

            logText = thresholdLog;
            return dstMatrix;
        } // ApplyDoubleGraylevelImageFilter()

        protected override PZMath_matrix[] ApplyDoubleColourImageFilter(PZMath_matrix[] srcMatrixArray, out string logText)
        {
            // TODO: add this function

            throw new Exception("The method or operation is not implemented.");
        } // ApplyDoubleColourImageFilter()

        #endregion
       
        /// <summary>
        /// binarize the source image
        /// true - 1 - black
        /// false - 0 - white
        /// </summary>
        /// <param name="srcMatrix">source matrix</param>
        /// <returns>bool image</returns>
        public bool[,] DoBinarize(PZMath_matrix srcMatrix)
        {
            return DoBinarize(srcMatrix, out _log);
        } // DoBinarize()       
        private bool[,] DoBinarize(PZMath_matrix srcMatrix, out string logText)
        {
            // get source
            int width = srcMatrix.ColumnCount;
            int height = srcMatrix.RowCount;
            bool[,] binaryImg = new bool[width, height];

            string thresholdLog = "";
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (srcMatrix[y, x] <= _threshold)
                        binaryImg[x, y] = true;     // true -- 1 -- black
                    else
                        binaryImg[x, y] = false;    // false -- 0 -- white
                }
            }

            logText = "Bianrization succeeds. " + thresholdLog;
            return binaryImg;
        } // DoBinarize()
    }
}
